package com.project.decoration.controller.product;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.decoration.common.enums.ResultCode;
import com.project.decoration.domain.DecorationProduct;
import com.project.decoration.domain.DecorationProductType;
import com.project.decoration.domain.Result;
import com.project.decoration.service.DecorationProductService;
import com.project.decoration.service.DecorationProductTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 分类controller
 * @date 2025/02/13 08:41
 */
@Controller
@ResponseBody
@RequestMapping("type")
public class DecorationProductTypeController {

    @Autowired
    private DecorationProductTypeService decorationProductTypeService;
    @Autowired
    private DecorationProductService decorationProductService;

    /** 分页获取分类 */
    @PostMapping("getDecorationProductTypePage")
    public Result getDecorationProductTypePage(@RequestBody DecorationProductType decorationProductType) {
        Page<DecorationProductType> page = new Page<>(decorationProductType.getPageNumber(),decorationProductType.getPageSize());
        QueryWrapper<DecorationProductType> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(decorationProductType.getName()),DecorationProductType::getName,decorationProductType.getName());
        Page<DecorationProductType> decorationProductTypePage = decorationProductTypeService.page(page, queryWrapper);
        return Result.success(decorationProductTypePage);
    }

    @GetMapping("getDecorationProductTypeList")
    public Result getDecorationProductTypeList() {
        List<DecorationProductType> typeList = decorationProductTypeService.list();
        return Result.success(typeList);
    }

    /** 根据id获取分类 */
    @GetMapping("getDecorationProductTypeById")
    public Result getDecorationProductTypeById(@RequestParam("id")String id) {
        DecorationProductType decorationProductType = decorationProductTypeService.getById(id);
        return Result.success(decorationProductType);
    }

    /** 保存分类 */
    @PostMapping("saveDecorationProductType")
    public Result saveDecorationProductType(@RequestBody DecorationProductType decorationProductType) {
        boolean save = decorationProductTypeService.save(decorationProductType);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑分类 */
    @PostMapping("editDecorationProductType")
    public Result editDecorationProductType(@RequestBody DecorationProductType decorationProductType) {
        boolean save = decorationProductTypeService.updateById(decorationProductType);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除分类 */
    @GetMapping("removeDecorationProductType")
    public Result removeDecorationProductType(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                DecorationProductType productType = decorationProductTypeService.getById(id);
                QueryWrapper<DecorationProduct> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(DecorationProduct::getType,productType.getName());
                int count = decorationProductService.count(queryWrapper);
                if (count > 0) {
                    return Result.fail("类型下存在产品,无法删除");
                }
                decorationProductTypeService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("分类id不能为空！");
        }
    }

}
